package com.xunwulian.web.controller;

import com.xunwulian.base.entity.Page;
import com.xunwulian.base.util.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Map;

public class BaseController {

    private MyLogUtil myLogUtil = new MyLogUtil(BaseController.class);

    private FileUtil fileUtil = new FileUtil();

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected ExcelUtil excelUtil = new ExcelUtil();

    /**
     * 得到PageData
     */
    public PageData getPageData() {
        return new PageData(this.getRequest());
    }

    /**
     * 得到ModelAndView
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();

        return request;
    }

    /**
     * 得到32位的uuid
     *
     * @return
     */
    public String get32UUID() {

        return UuidUtil.get32UUID();
    }

    /**
     * 得到分页列表的信息
     */
    public Page getPage() {

        return new Page();
    }

    public static void logBefore(Logger logger, String interfaceName) {
        logger.info("");
        logger.info("start");
        logger.info(interfaceName);
    }

    public static void logAfter(Logger logger) {
        logger.info("end");
        logger.info("");
    }

    protected <T> void downloadExcel(HttpServletResponse response,
                                     String root,
                                     String filename,
                                     Collection<T> entities,
                                     Map<String, String> names)
    {

        try {
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename, "utf8"));
            response.setContentType(fileUtil.getFileContentType(filename));
            response.setCharacterEncoding("utf8");
            OutputStream os = response.getOutputStream();

            File fileRoot = new File(root);
            if (!fileRoot.exists() || !fileRoot.isDirectory()) {
                fileRoot.mkdirs();
            }
            File fileExcel = new File(root, filename);
            Workbook workbook = excelUtil.parseEntities(entities, names);
            excelUtil.writeToFile(workbook, fileExcel);

            myLogUtil.log(fileExcel.getCanonicalPath());

            FileInputStream fis = new FileInputStream(fileExcel);

            byte[] cache = new byte[1024];
            while (fis.available() != 0) {
                int length = fis.read(cache, 0, cache.length);
                os.write(cache, 0, length);
            }

            fis.close();
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}